import { _decorator, director, easing, Layout, Node, tween, v3, Vec3 } from 'cc';
import { UIManager } from '../../../framework/ui/uimanager/UIManager';
import { VibrateManager } from '../../../common/src/manager/VibrateManager';
import { AudioManager } from '../../../common/src/manager/AudioManager';
import { MonoUIPanel } from '../../../../project-base/src/scripts/MonoUIPanel';
import { SETTING_TYPE } from '../../../utils/Enums';
const { ccclass, property } = _decorator;

@ccclass('SettingPanel')
export class SettingPanel extends MonoUIPanel {
    @property({ type: Layout, tooltip:"内容布局."})
    content: Layout = null;   

    @property({ type: Node, tooltip:"节点-声音."})
    soundNode: Node = null;

    @property({ type: Node, tooltip:"节点-震动."})
    vibrateNode: Node = null;

    @property({ type: Node, tooltip: "节点-音乐." })
    musicNode: Node = null;

    @property({ type: Node, tooltip: "按钮-首页." })
    btnHome: Node = null;

    @property({ type: Node, tooltip: "按钮-重开." })
    btnRestart: Node = null;
    
    @property({ type: Node, tooltip: "启动时" })
    startType: Node = null;
    
    @property({ type: Node, tooltip: "游戏时" })
    gameType: Node = null;
    // =======================================
    // 外部/内部属性定义(以public/private修饰)
    // =======================================
    // 数据对象缓存
    private data = null;

    private configs = {
        0: {
            content: { height: 323, paddingTop: 38, spacingY: 41 },
            bottomBtn: { active: true }
        },
        1: {
            content: { height: 402, paddingTop: 71, spacingY: 58 },
            bottomBtn: { active: false }
        },
    };

    private info: {
        type: SETTING_TYPE
    }

    private tempScale = new Vec3();
    // =======================================
    // 生命周期(模板方法，以on开头)
    // =======================================
    
    onStart(){
        this.initUI();
    }
    
    onEndabled() {
        this.updateSoundState();
        this.updateVibrateState();
        this.updateMusicState();
        this.initUI();
    }
    // =======================================
    // 自定义事件回调(以on开头)
    // =======================================

    /**关闭 */
    public onClickClose() {
        AudioManager.playButton();
        this.onClosePanel();
    }
 
     /**点击音效 */
     onClickSound() {
        AudioManager.enable = !AudioManager.enable;
        AudioManager.playButton();
        this.updateSoundState(true);
    }

    /**点击音乐 */
    onClickMusic() {
        AudioManager.playButton();
        AudioManager.musicEnable = !AudioManager.musicEnable;

        
        this.updateMusicState(true);
    }

    /**点击震动 */
    onClickShake() {
        AudioManager.playButton();
        VibrateManager.enable = !VibrateManager.enable;
        VibrateManager.vibrateShort();
        this.updateVibrateState(true);
    }

    /**回到主页 */
    onClickHome() {
        AudioManager.playButton();
        this.onClosePanel()
    
        director.loadScene("start")
    }

    /**重新游戏 */
    onClickRestartGame() {
        AudioManager.playButton();

        // gg.systemEvent.emit(EVENT_NAMES.GAME_RESTART)
        this.onClosePanel()
    }
    
    // =======================================
    // 游戏逻辑方法(内部调用的用private修饰，外部调用和编辑器绑定的public修饰，废弃的方法不加修饰符方便后期移除)
    // =======================================

    // /** 设置数据 */
    // setData(data) {
    //     this.data = data;
    //     this.initUI();
    // }

    
    public async setData(data) {

        console.log ("设置内容")
        this.info = data

        this.initUI()
    }
       /** 关闭面板. */
       public async onClosePanel() {
        await this.onExitAnimation();
        UIManager.close("setting:res/prefab/SettingPanel");
    }
    //界面打开的动画
    protected onEnterAnimation() {
        this.tempScale.set(0.01, 0.01, 1);
        this.dialog.scale = this.tempScale;
        tween(this.dialog)
        .call(() => { this.tempScale.set(1.08, 1.08, 1) })
        .to(0.2, { scale: this.tempScale }, { easing: easing.sineOut })
        .call(() => { this.tempScale.set(1, 1, 1) })
        .to(0.1, { scale: this.tempScale }, { easing: easing.sineIn })
        .start();
    }
    // 界面关闭的动画
    protected onExitAnimation(callback?) {
        return new Promise<void>((resolve) => {
            tween(this.dialog)
                .call(() => { this.tempScale.set(1.08, 1.08, 1) })
                .to(0.1, { scale: this.tempScale }, { easing: easing.quadOut })
                .call(() => { this.tempScale.set(0.01, 0.01, 1) })
                .to(0.2, { scale: this.tempScale }, { easing: easing.quadIn })
                .call(() => {
                    callback && callback();
                    resolve();
                })
                .start();
        });
    }
    /** 更新声音状态. */
    private updateSoundState(isAnimation = false) {
        const index = AudioManager.enable ? 1 : 0;

        let onNode = this.soundNode.children[2],
            onLabel = this.soundNode.children[0],
            offNode = this.soundNode.children[3],
            offLabel = this.soundNode.children[1];

        let tempOnPos = v3(42, -2.5, 0);
        let tempOffPos = v3(-42, -2.5, 0);
        if (isAnimation) {
            onNode.setPosition(tempOnPos);
            offNode.setPosition(tempOffPos);
            if (index) {
                tween(offNode).to(0.2, { position: tempOnPos })
                    .call(() => {
                        onNode.active = true;
                        onLabel.active = true;
                        offNode.active = false;
                        offLabel.active = false;
                    }).start();
            }
            else {
                tween(onNode).to(0.2, { position: tempOffPos })
                    .call(() => {
                        offNode.active = true;
                        offLabel.active = true;
                        onNode.active = false;
                        onLabel.active = false;
                    }).start();
            }
        } else {
            onNode.active = onLabel.active = !!index;
            offNode.active = offLabel.active = !index;
            onNode.setPosition(tempOnPos);
            offNode.setPosition(tempOffPos);
        }
    }

    /** 更新音乐状态. */
    private updateMusicState(isAnimation = false) {
        const index = AudioManager.musicEnable ? 1 : 0;

        let onNode = this.musicNode.children[2],
            onLabel = this.musicNode.children[0],
            offNode = this.musicNode.children[3],
            offLabel = this.musicNode.children[1];

        let tempOnPos = v3(42, -2.5, 0);
        let tempOffPos = v3(-42, -2.5, 0);
        if (isAnimation) {
            onNode.setPosition(tempOnPos);
            offNode.setPosition(tempOffPos);
            if (index) {
                tween(offNode).to(0.2, { position: tempOnPos })
                    .call(() => {
                        onNode.active = true;
                        onLabel.active = true;
                        offNode.active = false;
                        offLabel.active = false;
                    }).start();
            }
            else {
                tween(onNode).to(0.2, { position: tempOffPos })
                    .call(() => {
                        offNode.active = true;
                        offLabel.active = true;
                        onNode.active = false;
                        onLabel.active = false;
                    }).start();
            }
        } else {
            onNode.active = onLabel.active = !!index;
            offNode.active = offLabel.active = !index;
            onNode.setPosition(tempOnPos);
            offNode.setPosition(tempOffPos);
        }
    }

    /** 更新震动状态. */
    private updateVibrateState(isAnimation = false) {
        const index = VibrateManager.enable ? 1 : 0;

        let onNode = this.vibrateNode.children[2],
            onLabel = this.vibrateNode.children[0],
            offNode = this.vibrateNode.children[3],
            offLabel = this.vibrateNode.children[1];

        let tempOnPos = v3(42, -2.5, 0);
        let tempOffPos = v3(-42, -2.5, 0);
        if (isAnimation) {
            onNode.setPosition(tempOnPos);
            offNode.setPosition(tempOffPos);
            if (index) {
                tween(offNode).to(0.2, { position: tempOnPos })
                    .call(() => {
                        onNode.active = true;
                        onLabel.active = true;
                        offNode.active = false;
                        offLabel.active = false;
                    }).start();
            }
            else {
                tween(onNode).to(0.2, { position: tempOffPos })
                    .call(() => {
                        offNode.active = true;
                        offLabel.active = true;
                        onNode.active = false;
                        onLabel.active = false;
                    }).start();
            }
        } else {
            onNode.active = onLabel.active = !!index;
            offNode.active = offLabel.active = !index;
            onNode.setPosition(tempOnPos);
            offNode.setPosition(tempOffPos);
        }
    }
     /** 初始化UI. */
    private initUI() {
        if(this.info){
            if (this.info.type == SETTING_TYPE.START) {
                this.startType.active = true
                this.gameType.active = false
            } else if (this.info.type == SETTING_TYPE.MAIN) {
                this.startType.active = false
                this.gameType.active = true
            }
        }
    }
}


